/**
 * 
 */
package com.doone.telmarket.infrastructure.dao.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.doone.telmarket.domain.Service;
import com.doone.telmarket.domain.ServicePage;
import com.doone.telmarket.domain.ServicePageContent;
import com.doone.telmarket.domain.ServiceProduct;
import com.doone.telmarket.infrastructure.dao.BaseDaoSupport;
import com.doone.telmarket.infrastructure.dao.ServicePageContentDao;
import com.doone.telmarket.infrastructure.dao.ServicePageDAO;
import com.doone.telmarket.infrastructure.dao.ServiceProductDAO;

/**
 * @author 吴同庆
 * @date 2009-10-9
 * @description 
 * @package com.doone.telmarket.infrastructure.dao.impl
 */

@Repository("servicePageDAO")
public class ServicePageDAOImpl extends BaseDaoSupport implements ServicePageDAO {

	@Autowired
	private ServiceProductDAO serviceProductDAO;
	@Autowired
	private ServicePageContentDao servicePageContentDao;
	/**
	 * @package com.doone.telmarket.domain.ibator
	 * @author 甘雨波
	 * @description 根据增值业务ID获取增值业务PAGEBEAN
	 * @date 2009-9-23
	 */
	public ServicePage selectServicePageByPrimaryKey(Integer serviceId) {
		return (ServicePage) getSqlMapClientTemplate().queryForObject("ServicePage.selectServicePageByPrimaryKey",
				serviceId);
	}

	/**
	 * @package com.doone.telmarket.domain.ibator
	 * @author 甘雨波
	 * @description 保存增值业务PAGEBEAN
	 * @date 2009-9-23
	 */
	public void insertServicePage(ServicePage servicePage) {
		getSqlMapClientTemplate().insert("ServicePage.insertServicePage", servicePage);
	}

	/**
	 * @package com.doone.telmarket.domain.ibator
	 * @author 甘雨波
	 * @description 更新增值业务
	 * @date 2009-9-23
	 */
	public void updateService(ServicePage servicePage) {
		Service service = servicePage.getService();
		getSqlMapClientTemplate().update("Service.updateService", service);
		getSqlMapClientTemplate().update("ServicePage.updateServicePage", servicePage);
		serviceProductDAO.deleteServiceProductByServiceCode(service.getServiceCode());
		servicePageContentDao.deleteByServicePageCode(new Integer(servicePage.getServiceCode()));
		List<ServicePageContent> spcList = servicePage.getContents();
		List<ServiceProduct> sproList = service.getProducts();
		for(int i=0;i<spcList.size();i++){
			servicePageContentDao.saveServicePageContent(spcList.get(i));
		}
		for(int z=0;z<sproList.size();z++){
			serviceProductDAO.saveServiceProduct(sproList.get(z));
		}
	}

	/**
	 * 删除ServiceCode关联的增值业务页面可编辑对象
	 */
	public int deleteServicePageContentByServiceCode(Integer serviceCode) {
		return getSqlMapClientTemplate().delete("ServicePage.deleteServicePageContentByServiceCode", serviceCode);
	}

	public int deleteServicePageByServiceCode(Integer serviceCode) {
		return (Integer)getSqlMapClientTemplate().delete("ServicePage.deleteServicePage", serviceCode);
	}

}
